<?php

/*
 * Created on 10/01/2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
class TeamDao {
    /**
     * Permanently deletes a team and all associated records
     */
    function delete($team) {
        // season teams
        executeQuery("delete from seasons_teams where fk_team_id=$team->id");

        // matches - using dao due to complex relationships
        $matchDao = new MatchDao();
        $matches = $matchDao->findTeamMatches($team->id);
        foreach ($matches as $match) {
            $matchDao->delete($match);
        }

        // players - live players still in team 
        $playerDao = new PlayerDao();
        foreach($team->players as $player) {
            $playerDao->delete($player);
        }
        // dead & retired players from the team
        $players = $playerDao->findTeamDeadPlayers($team->id);
        foreach($players as $player) {
            $playerDao->delete($player);
        }
        
        // teams
        executeQuery("delete from teams where team_id=$team->id");
    }
    
    function findCoachTeams($coach) {
    	global $teams;
    	$coachTeams = array();
        $rs = executeQuery("select team_id, name from teams where fk_coach_id=$coach->id order by last_update desc, name");
        while ($row = mysql_fetch_row($rs)) {
            $team = $teams->getTeam($row[0]);
            $coachTeams[$team->id] = $team; 
        }
        return $coachTeams;
    }
}
?>
